<?php

namespace App\Http\Controllers;

use App\Models\Agent\ChaoZongdai;
use App\Models\Role;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function filters(Request $request, $model) {

        $filters = $request->get('filters');

        if ($filters) {
            $filters = json_decode($filters, true);

            if ($filters['rules']) {

                $or = $filters['groupOp'] == "OR" ? true : false;

                $filters = $filters['rules'];
                foreach ($filters as $filter) {
                    $op = '=';
                    switch ($filter['op']) {
                        case 'eq': $op = '='; break;
                        case 'ne': $op = '!='; break;
                        case 'lt': $op = '<'; break;
                        case 'le': $op = '<='; break;
                        case 'gt': $op = '>'; break;
                        case 'ge': $op = '>='; break;
                        case 'bw': {
                            $op = 'like';
                            $filter['data'] = "{$filter['data']}%";
                            break;
                        }
                        case 'bn': {
                            $op = 'not like';
                            $filter['data'] = "{$filter['data']}%";
                            break;
                        }
                        case 'in': {
                            if ($or) {
                                $model = $model->orWhereIn($filter['field'], explode(",", $filter['data']));
                            } else {
                                $model = $model->whereIn($filter['field'], explode(",", $filter['data']));
                            }
                            continue 2;
                        }
                        case 'ni': {
                            if ($or) {
                                $model = $model->orWhereNotIn($filter['field'], explode(",", $filter['data']));
                            } else {
                                $model = $model->whereNotIn($filter['field'], explode(",", $filter['data']));
                            }
                            continue 2;
                        }
                        case 'ew': {
                            $op = 'like';
                            $filter['data'] = "%{$filter['data']}";
                            break;
                        }
                        case 'en': {
                            $op = 'not like';
                            $filter['data'] = "%{$filter['data']}";
                            break;
                        }
                        case 'cn': {
                            $op = 'like';
                            $filter['data'] = "%{$filter['data']}%";
                            break;
                        }
                        case 'nc': {
                            $op = 'not like';
                            $filter['data'] = "%{$filter['data']}%";
                            break;
                        }

                    }

                    if ($or) {
                        $model = $model->orWhere($filter['field'], $op, $filter['data']);
                    } else {
                        $model = $model->where($filter['field'], $op, $filter['data']);
                    }
                }
            }
        }
        return $model;
    }

    protected function isChaoZongdai() :bool {

        $admin = Auth::user();
        $is_chaozong = Role::find($admin->role_id)->name == "合伙人" ? true : false;
        return $is_chaozong;
    }

//    protected function isAllSp() :bool {
//
//        $admin = Auth::user();
//        $admin_id = $admin->user_id;
//        return UserPlatform::isAll($admin_id);
//    }

    protected function isSameChaoZongdai(int $chaoZongdaiId) :bool {

        $admin = Auth::user();

        if (Role::find($admin->role_id)->name == "合伙人") {
            $chaoz = ChaoZongdai::where("email", $admin->email)->first();
            if ($chaoz->id != $chaoZongdaiId)
                return false;
        }
        return true;
    }

//    protected function isSameSp(int $userId) :bool {
//
//        $admin = Auth::user();
//        $admin_id = $admin->user_id;
//
//        if (!UserPlatform::isAll($admin_id) && Role::find($admin->role_id)->name != "合伙人") {
//            $acc = User::find($userId);
//            if (!in_array($acc->SPID, UserPlatform::ids($admin_id))) {
//                return false;
//            }
//        }
//        return true;
//    }
}
